package com.wanqing.mapper;

import com.github.pagehelper.Page;
import com.wanqing.annotation.AutoFill;
import com.wanqing.dto.UserVoucherPageQueryDTO;
import com.wanqing.dto.VoucherPageQueryDTO;
import com.wanqing.entity.UserVoucher;
import com.wanqing.entity.Voucher;
import com.wanqing.entity.VoucherRule;
import com.wanqing.enumeration.OperationType;
import com.wanqing.vo.UserVoucherVO;
import com.wanqing.vo.VoucherVO;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface VoucherMapper {
    /**
     * 新增优惠券
     */
    /**
     * 新增优惠券信息，并返回主键
     *
     * @param voucher 优惠券对象
     * @return 优惠券id
     */
    @AutoFill(OperationType.INSERT)
    int insertVoucher(Voucher voucher);

    /**
     * 插入一个新的优惠券规则
     *
     * @param voucherRule 要插入的优惠券规则对象
     */
    void insertRule(VoucherRule voucherRule);

    /**
     * 根据优惠券ID选择优惠券信息对象
     *
     * @param id 优惠券ID
     * @return 对应ID的优惠券信息对象
     */
    VoucherVO selectVoucherVOById(Long id);

    /**
     * 选择优惠券信息列表根据指定的分页查询条件
     *
     * @param voucherPageQueryDTO 分页查询条件对象
     * @return 根据查询条件获取的优惠券信息列表
     */
    Page<VoucherVO> selectVoucherVOList(VoucherPageQueryDTO voucherPageQueryDTO);

    /**
     * 更新优惠券信息通过优惠券ID
     *
     * @param voucher 包含更新信息的优惠券对象
     */
    @AutoFill(OperationType.UPDATE)
    void updateVoucherById(Voucher voucher);

    /**
     * 更新指定优惠券ID的优惠券规则
     *
     * @param voucherRule 优惠券规则
     */
    void updateRuleById(VoucherRule voucherRule);

    void deleteBatch(List<Long> ids);

    /**
     * 根据优惠券ID查询优惠券信息对象
     *
     * @param id 优惠券ID
     * @return 对应ID的优惠券信息对象
     */
    @Select("select * from voucher where id = #{id}")
    Voucher getById(Long id);

    void deleteRuleBatch(List<Long> ids);

    /**
     * 扣减优惠券的库存
     *
     * @param voucherId 优惠券ID
     * @return 扣减结果，成功返回1，失败返回0
     */
    @Update("update voucher set stock = stock - 1 where id = #{voucherId} and stock > 0")
    int reduceStock(Long voucherId);

    /**
     * 根据优惠券ID查询优惠券规则对象
     *
     * @param voucherId 优惠券ID
     * @return 对应ID的优惠券规则对象
     */
    @Select("select * from voucher_rule where voucher_id = #{voucherId}")
    VoucherRule getRuleByVoucherId(Long voucherId);

    /**
     * 查询指定用户ID和优惠券ID的领取数量
     *
     * @param userId    用户ID
     * @param voucherId 优惠券ID
     * @return 领取数量
     */
    @Select("select count(1) from user_voucher where user_id = #{userId} and voucher_id = #{voucherId}")
    int getReceivedCount(Long userId, Long voucherId);

    @Select("select * from voucher where id = #{voucherId}")
    Voucher selectById(Long voucherId);

    @Insert("INSERT INTO user_voucher (user_id, voucher_id, receive_time, valid_start_time, valid_end_time, status, use_time, order_id, source, remark, create_time, update_time) " +
            "VALUES (#{userId}, #{voucherId}, #{receiveTime}, #{validStartTime}, #{validEndTime}, #{status}, #{useTime}, #{orderId}, #{source}, #{remark}, #{createTime}, #{updateTime})")
    void insertUserVoucher(UserVoucher userVoucher);

    Page<UserVoucherVO> selectUserVoucherVOList(UserVoucherPageQueryDTO userVoucherPageQueryDTO);

    @Select("select * from user_voucher where valid_end_time < now() and status=0")
    List<UserVoucher> checkExpiredUserVoucher();

    /**
     * 更新用户优惠券状态 使用 过期
     *
     * @param userVoucher
     * @return
     */
    int updateUserVoucherStatus(UserVoucher userVoucher);

    @Select("select * from voucher where valid_end_time < now() and status=1")
    List<Voucher> checkExpiredVoucher();

    @Select("select voucher_id from user_voucher where id = #{id}")
    Long getVoucherId(Long id);

    @Select("select * from voucher_rule where voucher_id = #{voucherId}")
    VoucherRule getVoucherRuleById(Long voucherId);


    @Update("update user_voucher set status = 0,use_time=null,order_id=null,update_time=NOW() where order_id = #{orderId}")
    void refundVoucher(Long orderId);

    void batchUpdateExpiredVoucher(@Param("ids")List<Long> voucherIds,  @Param("status")byte status);

    void batchUpdateExpiredUserVoucherStatus(@Param("ids")List<Long> expiredUserVoucherIds,  @Param("status")byte status);
}